/*
 * Featureous is distributed under the GPLv3 license.
 *
 * University of Southern Denmark, 2011
 */
package dk.sdu.mmmi.featureous.explorer.api;

import dk.sdu.mmmi.featureous.core.controller.Controller;
import dk.sdu.mmmi.featureous.core.model.TraceListChangeListener;
import dk.sdu.mmmi.featureous.core.model.TraceSet;
import dk.sdu.mmmi.featureous.explorer.spi.FeatureTraceView;
import dk.sdu.mmmi.featureous.utils.TraceChangedAggregator;
import java.awt.Image;
import org.openide.util.ImageUtilities;
import org.openide.windows.TopComponent;

/**
 * TODO: implement selection change notification for exposing to subclasses
 *
 * @author ao
 */
public abstract class AbstractTraceView extends TopComponent implements FeatureTraceView {

    private String name = "to be set in the constructor";
    private String ICON_PATH = "fugue/icons/puzzle.png";

    /** Creates new form GenericTraceView */
    public AbstractTraceView() {
        initComponents();
    }

    /**
     * 
     * @param name
     * @param tooltip
     * @param ICON_PATH Path to a 32x32 PNG icon. In order to get a small icon in the
     * editor tab you also need to have 16x16 icon on the same path. e.g.
     * foo/bar.png + foo/bar16.png
     */
    public void setupAttribs(final String name, final String tooltip, final String ICON_PATH) {
        this.ICON_PATH = ICON_PATH;
        this.name = name;

        if (ICON_PATH != null) {
            setIcon(ImageUtilities.loadImage(ICON_PATH.replace(".png", "16.png"), false));
        } else {
            setIcon(ImageUtilities.loadImage(this.ICON_PATH, false));
        }
        setToolTipText(tooltip);
        setName(name);
    }

    public Image getBigIcon(){
        return ImageUtilities.loadImage(ICON_PATH, false);
    }
    
    public abstract TopComponent createInstance();

    /**
     * Called when view is graphically opened, before any listeners to core
     * of Featureous are registered.
     */
    public abstract void createView();

    /**
     * Called whenever there is a change in the set of traces.
     */
    public void updateView(){
        // do nothing by default
    };

    public String getGuiMode(){
        return "editor"; //editor placement by default
    }
    
    /**
     * Called when view is graphically closed, after all listeners to core
     * of Featureous are unregistered.
     */
    public abstract void closeView();

    @Override
    public AbstractTraceView getInstance() {
        return this;
    }

    @Override
    public int getPersistenceType() {
        return PERSISTENCE_NEVER;
    }

    @Override
    public void componentOpened() {
        createView();
        setFocusable(true);
//        UIUtils.setupDefaultColorForAll(this);
        Controller.getInstance().getTraceSet().addChangeListener(tcl);
    }

    @Override
    public void componentClosed() {
        Controller.getInstance().getTraceSet().removeChangeListener(tcl);
        closeView();
    }
    private TraceListChangeListener tcl = new TraceChangedAggregator(this) {

        public void traceListChangedEvent(TraceSet tl) {

            updateView();

            repaintView();
        }
    };
    
    public void repaintView(){
        invalidate();
        revalidate();
        updateUI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
